﻿using AutoMapper;
using MediatR;
using Swimj.CommonLib.Base;
using BaseData.CommonLib.Models;
using BaseData.DomainLib.AggregatesModel.WarehouseAggregate;

namespace BaseData.ApplicationLib.Commands;

public class CreateWarehouseCommand : WarehouseEditRequestDto, IRequest<Result<WarehouseInfoResponseDto>>
{
}

public class CreateWarehouseCommandHandler : IRequestHandler<CreateWarehouseCommand, Result<WarehouseInfoResponseDto>>
{
    private readonly IWarehouseRepository _processRepository;
    private readonly IMapper _mapper;

    public CreateWarehouseCommandHandler(IWarehouseRepository processRepository,
        IMapper mapper)
    {
        _processRepository = processRepository;
        _mapper = mapper;
    }

    public async Task<Result<WarehouseInfoResponseDto>> Handle(CreateWarehouseCommand request,
        CancellationToken cancellationToken)
    {
        var process = new DomainLib.AggregatesModel.WarehouseAggregate.Warehouse(request.Code, request.Name, request.WarehouseType, request.Address,
            request.Description);
        _processRepository.Add(process);

        await _processRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

        return await Result<WarehouseInfoResponseDto>.SuccessAsync(
            _mapper.Map<WarehouseInfoResponseDto>(process));
    }
}